package cn.dgut.FrontManger;

import cn.dgut.Entity.Student;
import cn.dgut.Utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/CheckRegister")
public class CheckRegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int exsit = 0;
        String name = request.getParameter("name");
        JDBCUtils utils = new JDBCUtils();
        List list = utils.queryAllData("Student");
        for (int i = 0; i < list.size(); i++) {
            Student stu = (Student) list.get(i);
            if(stu.getStudent_name().equals(name.trim())){
                exsit = 1;
                break;
            }
        }
        response.setContentType("application/json,charset=utf-8");
        String strJson = "{\"exsit\":\""+exsit+"\"}";
        response.getWriter().println(strJson);
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}